<?php

namespace App\Admin\Controllers;

use App\Models\Car;
use App\Models\VehicleMaintenance;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class VehicleMaintenanceController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('车辆检修记录');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {


            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(VehicleMaintenance::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->id('ID')->sortable();
            $grid->disableFilter();
            $grid->disableExport();
            $grid->car_id('车牌号')->display(function ($val){
                return Car::pluck('license_plate_number','id')[$val];
            });
            $grid->name('维护人');
            $grid->charge('负责人');
            $grid->adr('检修地点');
            $grid->time('检修时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(VehicleMaintenance::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->select('car_id', '车辆')->options(Car::pluck('license_plate_number','id'))->rules('required',[
                'required'=>'该选项必须填写'
            ]);
            $form->date('time', '检修时间');
            $form->text('adr', '检修地点');
            $form->text('name', '维护人');
            $form->text('charge', '负责人');
        });
    }
}
